#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <unistd.h>

#include "protocol.h"

#define PORT 8852

int sockfd;
int connected;

void printPlayer(int);
void printRoom(int);

	Player* p[15];
	Room* rm[15];
	int numRm = 0, numP = 0;

void *thread0(void *param)
{
	int len, i;
	CODE cmd;

	while(connected) {
		read(sockfd, &cmd, sizeof(CODE));
printf("Recived command: %d\n", cmd);
		switch(cmd) {
		case SERVERFULL:
			printf("Server is full.\n");
			connected = 0;
			break;
		case ADMIN:
		case ACCEPTED:
			read(sockfd, &cmd, sizeof(int));
			break;
		case KICK:
			printf("Server Kicked you off.\n");
			connected = 0;
			break;
		case JOIN:
			read(sockfd, &cmd, sizeof(int));
			printf("Room %d now has ", cmd);
			read(sockfd, &cmd, sizeof(int));
			printf("%d as a member.\n", cmd);
			break;

		case LEAVE:
			read(sockfd, &cmd, sizeof(int));
			printf("Room %d now has ", cmd);
			read(sockfd, &cmd, sizeof(int));
			printf("%d as one less member.\n", cmd);
			break;

		case ROOM:
			rm[numRm] = calloc(1, sizeof(Room));

			printf("Server is sending room.\n");

			read(sockfd, &len, sizeof(int));
			rm[numRm]->name = malloc(len);
			read(sockfd, rm[numRm]->name, len);
			read(sockfd, &rm[numRm]->ID, sizeof(int));
			read(sockfd, &rm[numRm]->gamesPlayed, sizeof(int));
			read(sockfd, &rm[numRm]->numberInRoom, sizeof(int));
			rm[numRm]->players = calloc(rm[numRm]->numberInRoom, sizeof(int));
			read(sockfd, &rm[numRm]->players, rm[numRm]->numberInRoom*sizeof(int));
			read(sockfd, &rm[numRm]->maxForRoom, sizeof(int));
			read(sockfd, &rm[numRm]->playerX, sizeof(int));
			read(sockfd, &rm[numRm]->playerO, sizeof(int));
			read(sockfd, &rm[numRm]->admin, sizeof(int));
			read(sockfd, rm[numRm]->moves, sizeof(rm[numRm]->moves));

			printRoom(numRm);
			numRm++;
			break;

		case PLAYER:
			printf("Server is sending Player.\n");

			p[numP] = calloc(1, sizeof(Player));
			
			read(sockfd, &len, sizeof(int));
			p[numP]->name = calloc(len, sizeof(char));
			read(sockfd, p[numP]->name, len);
			read(sockfd, &p[numP]->ID, sizeof(int));
			read(sockfd, &p[numP]->gamesPlayed, sizeof(int));
			read(sockfd, &p[numP]->wins, sizeof(int));
			read(sockfd, &p[numP]->losses, sizeof(int));

			printPlayer(numP);
			numP++;
			break;
		default:
			printf("Server sent unkown command.\n");
			break;

		}
	}

printf("Leaving.\n");
	return param;
}

void printRoom(int i) {
	printf("Recieved Room: %s\n", rm[i]->name);
	printf("Room id: %d\n", rm[i]->ID);
}

void printPlayer(int i) {
	printf("Recieved Player: %s\n", p[i]->name);
}

int main(int argc, char* argv[])
{
	struct sockaddr_in serv_addr;
	char toServer[BUFSIZ];
	pthread_t thread;
	CODE cmd;

	if (argc != 2)
	{
		fprintf(stderr, "Correct usage: %s Address\n", argv[0]);
		exit(1);
	}

	if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
	{
		fputs("Socket returned null\n", stderr);
		exit(2);
	}

	memset(&serv_addr, 0, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_port = htons(PORT);
	serv_addr.sin_addr.s_addr = inet_addr(argv[1]);

	if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
	{
		fputs("Connection error\n", stderr);
		exit(3);
	}

	connected = 1;
	pthread_create(&thread, NULL, thread0, 0);

	/*
	 * Ask for user name, send to server first
	 * off (before initializing Mug)
	 */
	getchar();
		printf("User name: ");
		fgets(toServer, sizeof(toServer), stdin);
		toServer[strlen(toServer)-1] = '\0';

		/*
		if(strlen(toServer) < MAX_CLIENT_NAME)
			break;
		else
			printf("Name must be under %d characters.\n", MAX_CLIENT_NAME);
		*/
	int len = strlen(toServer) + 1;
	cmd = USERNAME;
	write(sockfd, &cmd, sizeof(CODE));
	write(sockfd, &len, sizeof(int));
	write(sockfd, toServer, len);
	fsync(sockfd);

	while (connected)
	{
		printf("Hit a key: Join 1\n");
		getchar();

		cmd = JOIN;
		write(sockfd, &cmd, sizeof(CODE));
		cmd = 1;
		write(sockfd, &cmd, sizeof(int));
		fsync(sockfd);

		printf("Hit a key: Leave 1\n");
		getchar();

		cmd = LEAVE;
		write(sockfd, &cmd, sizeof(CODE));
		cmd = 1;
		write(sockfd, &cmd, sizeof(int));
		fsync(sockfd);


	}

	close(sockfd);

	return 0;
}
